package com.UdpRecieve.controller.system;

import com.UdpRecieve.domain.BusinessLog;
import com.UdpRecieve.domain.system.SysOperLog;
import com.UdpRecieve.framework.page.TableDataInfo;
import com.UdpRecieve.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class LogController extends BaseController{


    @Autowired
    private SysLogService sysLogService;

    /**
     * 跳转业务日志页面
     * @return
     */
    @GetMapping("/logBusiness")
    public ModelAndView toLogBusiness(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("system/log_business");
        return mv;
    }


    /**
     * 查询和显示业务日志列表
     * @param businessLog
     * @return
     */
    @PostMapping("/logBusiness/list")
    @ResponseBody
    public TableDataInfo businessList(BusinessLog businessLog){
        startPage();
        List<BusinessLog> businessLogList =  sysLogService.selectBusinessLogList(businessLog);

        for (BusinessLog l:businessLogList){
            String[] r = l.getBusinessResult().split("_");
            if (r.length>2){
                l.setSaveResult(r[0]=="0"?"失败":"成功");//落地
                l.setLevel(r[1]=="0"?"失败":"成功");//密级
                l.setStatus(r[2]=="0"?"失败":"成功");//状态
                l.setExtend(r[3]=="0"?"失败":"成功");//扩展
                l.setDrafer(r[4]=="0"?"失败":"成功");//起草人
                l.setAuthority(r[5]=="0"?"失败":"成功");//定密人
                l.setCheckResult(r[6]=="0"?"失败":"成功");//验标
                l.setSecretSolveResult(r[7]=="0"?"失败":"成功");//解标
                l.setCopySingle(r[8]=="0"?"失败":"成功");//拷贝
            }else{
                l.setSaveResult(r[0]=="0"?"失败":"成功");//落地
                l.setCopySingle(r[1]=="0"?"失败":"成功");//拷贝
                l.setLevel("无");//密级
                l.setStatus("无");//状态
                l.setExtend("无");//扩展
                l.setDrafer("无");//起草人
                l.setAuthority("无");//定密人
                l.setCheckResult("无");//验标
                l.setSecretSolveResult("无");//解标
            }

        }

        return getDataTable(businessLogList);
    }

    /**
     * 删除业务日志通过id
     * @param ids
     * @return
     */
    @PostMapping("/logBusiness/delete")
    @ResponseBody
    public ModelMap deleteBusinessLog(String ids){
        ModelMap m = new ModelMap();
        int i = -1;
        try {
            i = sysLogService.deleteBusinessLogByIds(ids);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        m.addAttribute("result",i);
        return m;
    }


    /**
     * 跳转操作日志页面
     * @return
     */
    @GetMapping("/logOperate")
    public ModelAndView OperateLogList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("system/log_operate");
        return mv;
    }


    /**
     * 查询操作日志
     * @return
     */
    @PostMapping("/logOperate/list")
    @ResponseBody
    public TableDataInfo toLogOperate(SysOperLog sysOperLog){
        startPage();
        List<SysOperLog> sysOperLogList =  sysLogService.selectOperateLogList(sysOperLog);
        return getDataTable(sysOperLogList);
    }



    /**
     * 删除业务日志通过id
     * @param ids
     * @return
     */
    @PostMapping("/logOperate/delete")
    @ResponseBody
    public ModelMap deleteOperaLog(String ids){
        ModelMap m = new ModelMap();
        int i = -1;
        try {
            i = sysLogService.deleteOperaLogByIds(ids);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        m.addAttribute("result",i);
        return m;
    }
}
